package com.zyj.test.month1.day15;

/**
 * @author Yijia Zhang
 * @create 2024-06-04 22:00
 */
public class code0029_2 {
    //快速乘法
    public int divide(int dividend, int divisor) {
        final int minInt = Integer.MIN_VALUE;
        if (dividend == minInt && divisor == -1) {
            return Integer.MAX_VALUE;
        }
        if (divisor == 1) {
            return dividend;
        }
        if (divisor == -1) {
            return -dividend;
        }

        boolean reverse = false;
        if (dividend > 0) {
            dividend = -dividend;
            reverse = !reverse;
        }
        if (divisor > 0) {
            divisor = -divisor;
            reverse = !reverse;
        }

        int res = 0;
        while (dividend <= divisor) {
            int k = 1;
            int tmp = divisor;
            while (tmp + tmp < 0 && tmp + tmp >= dividend) {
                tmp = tmp + tmp;
                k = k + k;
            }
            res += k;
            dividend = dividend - tmp;
        }
        return reverse ? -res : res;



    }
}
